//
//  AppDelegate.m
//  Topit
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "SearchViewController.h"
#import "GroupViewController.h"
#import "ResumeViewController.h"
#import "CameraViewController.h"
#import "YXYSkipViewController.h"
//#import "UMSocialQQHandler.h"
#import "LeadViewController.h"
@interface AppDelegate ()<Leaddelegate>
@property (nonatomic, retain) LeadViewController *Lead;
@property (nonatomic, retain) UITabBarController *tabBarController;
@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
//    [UMSocialData setAppKey:@"5674be1867e58e561d001340"];
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    //主页
    ViewController *viewController = [[ViewController alloc] init];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:viewController];
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"home"] selectedImage:[[UIImage imageNamed:@"homeRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//    [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(-5, -50)];
    [viewController release];
    
    //搜索
    SearchViewController *searchViewController = [[SearchViewController alloc] init];
    UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    searchViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"search"] selectedImage:[[UIImage imageNamed:@"searchRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [searchViewController release];
    
    //相机
    CameraViewController *cameraViewController = [[CameraViewController alloc] init];
    UINavigationController *cameraNav = [[UINavigationController alloc] initWithRootViewController:cameraViewController];
    cameraViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[[UIImage imageNamed:@"camera"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"selectedcamera"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [cameraViewController release];
    
    //分组
    GroupViewController *groupViewController = [[GroupViewController alloc] init];
    UINavigationController *groupNav = [[UINavigationController alloc] initWithRootViewController:groupViewController];
    groupViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"group"] selectedImage:[[UIImage imageNamed:@"groupRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [groupViewController release];
    
    //个人
    ResumeViewController *resumeViewController = [[ResumeViewController alloc] init];
    UINavigationController *resumeNav = [[UINavigationController alloc] initWithRootViewController:resumeViewController];
    resumeViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"resume"] selectedImage:[[UIImage imageNamed:@"resumeRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [resumeViewController release];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(tabBarColor:) name:@"tabbar" object:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[homeNav, searchNav, cameraNav, groupNav, resumeNav];
    _window.rootViewController = self.tabBarController;
    
    
    self.Lead = [[LeadViewController alloc]init];
    self.Lead.delegate = self;
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        _window.rootViewController = self.Lead;
    }else{
        self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
        _window.rootViewController = self.tabBarController;
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *mode = [user objectForKey:@"NightMode"];
    if ([mode isEqualToString:@"Night"]) {
        self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    }else {
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    }
    
    [cameraNav release];
    [homeNav release];
    [searchNav release];
    [groupNav release];
    [resumeNav release];
    [_tabBarController release];
    
//    UITabBarController *tabarController = [[UITabBarController alloc] init];
    
    return YES;
}

- (void)tabBarColor:(NSNotification *)messager {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *mode = [user objectForKey:@"NightMode"];
    if ([mode isEqualToString:@"Night"]) {
        self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    }else {
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    }

}

- (void)chooseVC{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        _window.rootViewController = self.Lead;
    }else{
        
        self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
        _window.rootViewController = self.tabBarController;
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
